local skel = fk.CreateSkill{
  name = "kpaI__dingxue",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["kpaI__dingxue"] = "订血",
  [":kpaI__dingxue"] = "限定技，你使用红色牌后，可进行一次拼点，若你赢，则复原其他体力值下的“订血”；"..
  "若你没赢，则调整手牌至四，且此技能于当前体力值下视为“天义”。",
  ["#kpaI__dingxue_ask"] = "你可以用 %arg 拼点，若你赢，则复原其他体力值下的“订血”；<br>"..
  "若你没赢，则调整手牌至四，且此技能于当前体力值下视为“天义”。",
  ["@kpaI__pujikpaI__tianyi-phase"] = "天义：",
  ["@kpaI__pujikpaI__dingxue"] = "订血：",

  ["$kpaI__dingxue1"] = "史者，古文若何？上简而下手，无谓首也！",
  ["$kpaI__dingxue2"] = "铁砚磨穿犹奋笔，金阶血溅不违心。",
  ["$kpaI__dingxue3"] = "墨凝血而成章，简负骨而立世。",
  ["$kpaI__dingxue4"] = "白骨为墨，丹心为砚，书至公之理于简。",
  ["$kpaI__dingxue5"] = "请助我一臂之力。",
}

skel:addEffect(fk.HpChanged,{
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    if player:getMark(skel.name..player.hp) > 0 then
      player.room:handleAddLoseSkills(player,"kpaI__tianyi")
    else
      player.room:handleAddLoseSkills(player,"-kpaI__tianyi")
    end
  end,
})

skel:addEffect(fk.CardUseFinished, {
  mute = true,
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 and
    --     玩家用牌             有技能                          限定技
    data.card.color == Card.Red and #data.card.subcards < 2 and not table.contains(player:getTableMark("@kpaI__puji"..skel.name),tostring(player.hp)) and
    --红色牌                        --非转化牌                   --再加个判断，主要处理桃
    not(player:getMark(skel.name..player.hp) > 0) and
    --已经赢了，视为天义
    table.find(player.room:getOtherPlayers(player, false), function (q) return player:canPindian(q,true) end)
    -- 有可以拼点对象
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(player.room:getOtherPlayers(player, false), function (q) return player:canPindian(q,true) end)
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#kpaI__dingxue_ask:::"..data.card.name,
      skill_name = skel.name,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, skel.name)
    local iiii = math.max(1,math.min(player.hp,5))
    player:broadcastSkillInvoke(skel.name,iiii)
    local room = player.room
    room:addTableMarkIfNeed(player,"@kpaI__puji"..skel.name,tostring(player.hp))
    local to = event:getCostData(self).tos[1]
    local rst = player:pindian({to},skel.name)
    if rst.results[to].winner == player then
      --赢，复原订血
      for i =1,player.maxHp do
        if i ~= player.hp then
          player:setMark(skel.name..i,0)
          room:removeTableMark(player,"@kpaI__puji"..skel.name,tostring(i))
          room:removeTableMark(player,"@kpaI__pujikpaI__tianyi-phase",tostring(i))
        end
      end
    else
      --没赢，调整至四，视为天义
      local n = player:getHandcardNum() - 4
      if n > 0 then
        local cards = room:askToDiscard(player, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = skel.name,
          cancelable = false,
        })
      else
        n = 4 - player:getHandcardNum()
        if n > 0 then
          player:drawCards(n, skel.name)
        end
      end
      --
      player:setMark(skel.name..player.hp,1)
      room:handleAddLoseSkills(player,"kpaI__tianyi")
    end
  end,
})

return skel